package br.sed.theenemy.presentation.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.sed.theenemy.business.exception.InvalidPermalinkException;
import br.sed.theenemy.business.utils.Utils;
import br.sed.theenemy.presentation.EnemyConstants;

/**
 * 
 * @author Julio CB
 * @email jcbjulio@gmail.com
 * 
 */
public class ProfileServlet extends HttpServlet {

	private static final long serialVersionUID = -3473849468456849381L;

	private static final String INVALID_PROFILE_ID = "Invalid Profile Id";
	private static final String EMPTY_PROFILE_ID = "Empty Profile Id";
	
	/**
     * @see HttpServlet#HttpServlet()
     */
    public ProfileServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String userProfileIdStr = null;
        try {
            userProfileIdStr = splitPermalinkParams(request);
        } catch (final InvalidPermalinkException e) {
        	if(EMPTY_PROFILE_ID.equals(e.getMessage())){
        		//redirect
                //final RequestDispatcher rd = request.getRequestDispatcher(PROFILE_ACTION);
                //rd.forward(request, response);
                response.sendRedirect(EnemyConstants.PROFILE_PERMALINK);
                return;
        	}
        	response.sendRedirect(EnemyConstants.PROFILE_PERMALINK);
            return;
        }
		
        final Long userProfileId = Long.valueOf(userProfileIdStr);

        response.sendRedirect(EnemyConstants.PROFILE_PERMALINK+"?id="+userProfileId);

	}
	
	/**
     * Split and validates permalink parameters.
     * 
     * @param req
     * @return
	 * @throws InvalidPermalinkException 
     */
    private String splitPermalinkParams(final HttpServletRequest req) throws InvalidPermalinkException {
        
        final String path = EnemyConstants.PRIFILE_PATH;
        final int paramsIdx = req.getRequestURI().lastIndexOf(path) + path.length();
        final String paths = req.getRequestURI().substring(paramsIdx);
        
        final String[] permalinkParams = paths.split("/");
        
        // validate permalink url
        final String idUserTimelineStr = permalinkParams[0];
        if("".equals(idUserTimelineStr)){
        	throw new InvalidPermalinkException(EMPTY_PROFILE_ID);
        }
        if (Utils.isNullOrEmptyOrNotNumber(idUserTimelineStr)) {
            throw new InvalidPermalinkException(INVALID_PROFILE_ID);
        }
        
        return idUserTimelineStr;
    }   
	
}
